package com.aaa.stock.controller.api;

import com.aaa.entity.StockProducer;
import com.aaa.entity.StockProvider;
import com.aaa.stock.service.ProducerService;
import com.aaa.stock.vo.ProducerVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@RestController
    @RequestMapping("/stock/api/producer")
public class ApiProducerController {
    @Autowired
    private ProducerService producerService ;
    //全查
    @PostMapping("/list")
    public Result<List<StockProducer>> list(){
        return producerService.getAll();
    }
    //分页查询
    @PostMapping("/list1/{currentPage}/{pageSize}")
    public Result<IPage<StockProducer>> list1(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody ProducerVo producerVo
    ) {
        System.out.println("=== === " + currentPage);
        System.out.println("=== === " + pageSize);
        System.out.println("medicinesrVo = " + producerVo);
//
        return producerService.getByPage(currentPage, pageSize, producerVo);
    }




        //添加,修改
    @PostMapping("addstu")
    public Result addstu(@RequestBody StockProducer stockProducer){
        if (Objects.nonNull(stockProducer.getProducerId())){
            stockProducer.setUpdateTime(LocalDateTime.now());
        }else {
            stockProducer.setCreateTime(LocalDateTime.now());
        }
        producerService.saveOrUpdate(stockProducer);
        return new Result (2000,"成功");
    }

    //删除
    @DeleteMapping("/{producerId}")
    public Result delStu( @PathVariable Integer producerId){
        producerService.removeById(producerId);
        return new Result(2000,"成功");
    }
    //批量删除
    @PostMapping("/del1")
    public Result delStu1( @RequestBody List<StockProducer> stockProducers){
        stockProducers.forEach((StockProducer stockProvider)->{
            boolean b = producerService.removeById(stockProvider.getProducerId());
        });
        return new Result(2000,"成功");
    }

}
